import java.util.ArrayList;
import java.util.List;

/**
 * @author lirns
 * @date 2021/11/25 15:32
 */
public class CommonTreeUtil {

    /**
     * 将list转为tree
     */
    public static CommonTree listToTreeAndOneWithIf(List<ICommonTree> iCommonTrees) {
        List<CommonTree> commonTrees = CommonTreeUtil.listToTreeWithIf(iCommonTrees);
        return listToTreeAndOne(commonTrees);
    }

    public static CommonTree listToTreeAndOne(List<CommonTree> CommonTrees) {
        List<CommonTree> commonTrees = CommonTreeUtil.listToTree(CommonTrees);
        if (commonTrees.size() == 1) {
            return commonTrees.get(0);
        }
        if (commonTrees.size() > 1) {
            CommonTree commonTree = new CommonTree();
            commonTree.setChildren(commonTrees);
            return commonTree;
        }
        throw new RuntimeException("iCommonTrees无法生成树");
    }

    /**
     * 将实现接口的类 转化成树
     */
    public static List<CommonTree> listToTreeWithIf(List<ICommonTree> commonTreeList) {
        List<CommonTree> rootNodeList = new ArrayList<CommonTree>();
        for (ICommonTree iCommonTree : commonTreeList) {
            CommonTree commonTree = iCommonTree.handleCommonTree();
            if (commonTree.getPid() == null) {
                rootNodeList.add(commonTree);
                listToTreeWithIf(commonTree, commonTreeList);
            }
        }
        return rootNodeList;
    }

    /**
     * 将实现接口的类 挂载到节点上
     */
    public static void listToTreeWithIf(CommonTree rootNode, List<ICommonTree> commonTreeList) {
        for (ICommonTree iCommonTree : commonTreeList) {
            CommonTree commonTree = iCommonTree.handleCommonTree();
            if (rootNode.getId().equals(commonTree.getPid())) {
                rootNode.getChildren().add(commonTree);
                listToTreeWithIf(commonTree, commonTreeList);
            }
        }
    }

    /**
     * 将实现接口的类 挂载到节点上
     */
    public static CommonTree listToTreeWithIf(ICommonTree rootNode, List<ICommonTree> commonTreeList) {
        CommonTree rootNodeTree = rootNode.handleCommonTree();
        for (ICommonTree iCommonTree : commonTreeList) {
            CommonTree commonTree = iCommonTree.handleCommonTree();
            if (rootNodeTree.getId().equals(commonTree.getPid())) {
                rootNodeTree.getChildren().add(commonTree);
                listToTreeWithIf(commonTree, commonTreeList);
            }
        }
        return rootNodeTree;
    }

    /**
     * 将继承CommonTree的类转成树
     */
    public static <TYPE extends CommonTree> List<TYPE> listToTree(List<TYPE> commonTreeList) {
        List<TYPE> rootNodeList = new ArrayList<TYPE>();
        for (TYPE commonTree : commonTreeList) {
            if (commonTree.getPid() == null) {
                rootNodeList.add(commonTree);
                listToTree(commonTree, commonTreeList);
            }
        }
        return rootNodeList;
    }

    /**
     * 将继承CommonTree的类转成树 挂载到节点上
     */
    public static <TYPE extends CommonTree> void listToTree(TYPE rootNode, List<TYPE> commonTreeList) {
        for (TYPE commonTree : commonTreeList) {
            if (rootNode.getId().equals(commonTree.getPid())) {
                rootNode.getChildren().add(commonTree);
                listToTree(commonTree, commonTreeList);
            }
        }
    }

}
